package com.xszx.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.xszx.service.ManaService;
import com.xszx.service.SystemService;
import com.xszx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import com.xszx.bean.EmpInfo;
import com.xszx.bean.Insurance;
import com.xszx.bean.InsuranceBase;
import com.xszx.bean.LoginUser;
import com.xszx.bean.Memo;
import com.xszx.bean.Position;
import com.xszx.bean.UserAttendance;
import com.xszx.bean.UserVocate;
import com.xszx.bean.Wage;
import com.xszx.bean.AddressList;
import com.xszx.bean.Attention;
import com.xszx.bean.DepInfo;
import com.xszx.bean.DepPosition;
import com.xszx.bean.EmpBasicInfo;

@Controller
@CrossOrigin
public class ManaController {
    @Autowired
    ManaService manaService;

    @Autowired
    SystemService systemService;

    @Autowired
    UserService userService;

    @RequestMapping("/toManaBody")
    public String toManaBody(Model model) {
        List<Attention> list = systemService.selectAttention();
        model.addAttribute("attList", list);
        System.out.println("通知信息" + list.toString());
        return "manaBody.html";
    }

    //员工信息管理操作
    @RequestMapping("/toEmpInfo")
    public String toEmpInfo() {
        return "empInfo.html";
    }

    @RequestMapping("/getEmpInfo")
    @ResponseBody
    public Map<String, Object> getEmpInfo() {
        List<EmpBasicInfo> list = manaService.selectEmpInfo();
        System.out.println("基本员工信息" + list.toString());
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", "0");
        map.put("data", list);
        map.put("msg", "");
        map.put("count", list.size());
        return map;
    }

    @RequestMapping("/searchEmp")
    @ResponseBody
    public Map<String, Object> searchEmp(@RequestParam String empNo) {
        List<EmpBasicInfo> list = manaService.selectEmpByNo(empNo);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", "0");
        map.put("data", list);
        map.put("msg", "");
        map.put("count", list.size());
        return map;
    }

    @RequestMapping("/addEmp")
    @ResponseBody
    public boolean addEmp(@RequestBody EmpInfo empInfo) {
        System.out.println("添加员工信息" + empInfo.toString());
        int i = manaService.addEmp(empInfo);
        if (i > 0) {
            return true;
        } else {
            return false;
        }
    }

    @RequestMapping("/editEmp")
    @ResponseBody
    public boolean editEmp(@RequestBody EmpInfo empInfo) {
        System.out.println("准备提交到数据库的修改的员工信息" + empInfo.toString());
        int i = manaService.updateEmp(empInfo);
        if (i > 0) {
            return true;
        } else {
            return false;
        }
    }

    @RequestMapping("delEmp")
    @ResponseBody
    public boolean delEmp(@RequestParam int id) {
        int i = manaService.deleteEmpInfo(id);
        if (i > 0) {
            return true;
        } else {
            return false;
        }
    }

    //五险一金操作
    @RequestMapping("/toInsurance")
    public String toInsurance() {
        return "insurance.html";
    }

    @RequestMapping("/toInsuranceMana")
    public String toInsuranceMana() {
        return "insuranceMana.html";
    }

    @RequestMapping("/addInsurance")
    @ResponseBody
    public boolean addInsurance(@RequestBody InsuranceBase insuranceBase) {
        Insurance insurance = new Insurance();
        double base = insuranceBase.getBase();
        insurance.setNo(insuranceBase.getNo());
        insurance.setEmpNo(insuranceBase.getEmpNo());
        insurance.setName(insuranceBase.getName());
        insurance.setMonth(insuranceBase.getMonth());
        insurance.setYanglao(base * 0.08);
        insurance.setYiliao(base * 0.005);
        insurance.setShiye(base * 0.002);
        insurance.setFund(base * 0.05);
        insurance.setGongshang(0);
        insurance.setShengyu(0);
        insurance.setPeramount(insurance.getYanglao() + insurance.getYiliao() + insurance.getShiye() + insurance.getFund() + insurance.getGongshang() + insurance.getShengyu());
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("empNo", insuranceBase.getEmpNo());
        map.put("month", insuranceBase.getMonth());

        int count = manaService.selectSameInsurance(map);
        System.out.println("相同记录数" + count);
        if (count <= 0) {
            int i = manaService.addInsurance(insurance);
            if (i > 0) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    @RequestMapping("/getAllInsurance")
    @ResponseBody
    public Map<String, Object> getAllInsurance(@RequestParam int limit, @RequestParam int page) {
        Map<String, Object> map = new HashMap<String, Object>();
        int offset = (page - 1) * limit;
        map.put("offset", offset);
        map.put("limit", limit);
        List<Insurance> list = manaService.selectAllInsurance(map);
        int count = manaService.selectInsuranceCount();
        map.put("count", count);
        map.put("data", list);
        map.put("code", "0");
        map.put("msg", "");
        return map;
    }

    @RequestMapping("/delInsurance")
    @ResponseBody
    public boolean delInsurance(@RequestParam int id) {
        int i = manaService.deleteInsurance(id);
        if (i > 0) {
            return true;
        } else {
            return false;
        }
    }

    @RequestMapping("/editInsurance")
    @ResponseBody
    public boolean editInsurance(@RequestBody Insurance insurance) {
        System.out.println(insurance.toString());
        insurance.setPeramount(insurance.getYanglao() + insurance.getYiliao() + insurance.getShiye() + insurance.getFund() + insurance.getGongshang() + insurance.getShengyu());
        int i = manaService.updateInsurance(insurance);
        if (i > 0) {
            return true;
        } else {
            return false;
        }
    }

    @RequestMapping("/searchInsuranceByEmpNo")
    @ResponseBody
    public Map<String, Object> searchInsuranceByEmpNo(@RequestParam String empNo) {
        List<Insurance> list = manaService.selectInsuranceByEmpNo(empNo);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("msg", "");
        map.put("code", "0");
        map.put("count", list.size());
        map.put("data", list);
        return map;
    }

    //部门管理操作
    @RequestMapping("/toDepInfo")
    public String toDepInfo() {
        return "depInfo.html";
    }

    @RequestMapping("/getDepInfo")
    @ResponseBody
    public Map<String, Object> getDepInfo() {
        List<DepInfo> list = manaService.selectDepInfo();
        System.out.println("部门信息：" + list.toString());
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", "0");
        map.put("data", list);
        map.put("msg", "");
        map.put("count", list.size());
        return map;
    }


    @RequestMapping("/addDep")
    @ResponseBody
    public boolean addDep(@RequestBody DepInfo depInfo) {
        int i = manaService.addDep(depInfo);
        if (i > 0) {
            return true;
        } else {
            return false;
        }
    }

    @RequestMapping("/editDep")
    @ResponseBody
    public boolean editDep(@RequestBody DepInfo depInfo) {
        int i = manaService.updateDep(depInfo);
        if (i > 0) {
            return true;
        } else {
            return false;
        }
    }

    @RequestMapping("/toDepPosition")
    public String toDepPosition() {
        return "depPosition.html";
    }

    @RequestMapping("/getDepPosition")
    @ResponseBody
    public Map<String, Object> getDepPosition(@RequestParam int limit, @RequestParam int page) {
        Map<String, Object> map = new HashMap<String, Object>();
        int offset = (page - 1) * limit;
        map.put("offset", offset);
        map.put("limit", limit);
        List<DepPosition> list = manaService.selectDepPosition(map);
        int count = manaService.selectDepPositionCount();
        map.put("count", count);
        map.put("code", "0");
        map.put("data", list);
        map.put("msg", "");
        return map;
    }

    @RequestMapping("/addDepPosition")
    @ResponseBody
    public boolean addDepPosition(@RequestBody DepPosition depPosition) {
        int count = manaService.selectSamePosition(depPosition.getPosition());
        if (count > 0) {
            return false;
        } else {
            int i = manaService.addDepPosition(depPosition);
            if (i > 0) {
                return true;
            } else {
                return false;
            }
        }

    }

    @RequestMapping("/editDepPosition")
    @ResponseBody
    public boolean editDepPosition(@RequestBody DepPosition depPosition) {
        int i = manaService.editDepPosition(depPosition);
        if (i > 0) {
            return true;
        } else {
            return false;
        }
    }

    @RequestMapping("/delDepPosition")
    @ResponseBody
    public boolean delDepPosition(@RequestParam int id) {
        int count = manaService.selectPositionNum(id);
        //	System.out.println(count);
        if (count > 0) {
            return false;
        } else {
            int i = manaService.deleteDepPosition(id);
            if (i > 0) {
                return true;
            } else {
                return false;
            }
        }


    }

    //打卡信息管理页面
    @RequestMapping("/toAttMana")
    public String toAttMana() {
        return "attMana.html";
    }


//	@RequestMapping("/addManaAttendance")
//    @ResponseBody
//    public boolean addManaVacate(@RequestBody UserAttendance userAttendance,HttpSession session) {
//    	LoginUser loginUser=(LoginUser)session.getAttribute("loginUser");
//    	userAttendance.setEmpNo(loginUser.getAccount());
//    	userAttendance.setUserName(loginUser.getUserName());
//    	userVacate.setStatus(0);
//    	int i=userDao.addVocate(userVacate);
//    	if(i>0) {
//    		return true;
//    	}else {
//    		return false;
//    	}
//    }

    //打卡信息增删改查
    @RequestMapping("/getAllAttendance")
    @ResponseBody
    public Map<String, Object> getAllAttendance(@RequestParam int limit, @RequestParam int page) {
        System.out.println("分页数据=" + limit + page);
        Map<String, Integer> map1 = new HashMap<String, Integer>();
        int offset = limit * (page - 1);
        map1.put("limit", limit);
        map1.put("offset", offset);
        Map<String, Object> map = new HashMap<String, Object>();
        List<UserAttendance> list = manaService.selectAllAttendance(map1);
        int count = manaService.selectAttdanceCount();
        System.out.println("打卡信息" + list.toString());
        map.put("code", "0");
        map.put("data", list);
        map.put("msg", "");
        map.put("count", count);
        return map;
    }

    @RequestMapping("/updateAttendance")
    @ResponseBody
    public boolean updateAttendance(@RequestBody UserAttendance userAttendance) {
        int i = manaService.updateAttendance(userAttendance);
        if (i > 0) {
            return true;
        } else {
            return false;
        }
    }

    @RequestMapping("/searchAttendanceByKeyWord")
    @ResponseBody
    public Map<String, Object> searchAttendanceByKeyWord(@RequestParam String likeEmpNo, @RequestParam String likeDate) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("empNo", likeEmpNo);
        map.put("date", likeDate);
        List<UserAttendance> list = manaService.searchAttendanceByKeyWord(map);
        System.out.println("搜索结果" + list.toString());
        map.put("code", "0");
        map.put("data", list);
        map.put("msg", "");
        map.put("count", list.size());
        return map;
    }

    //请假管理

    @RequestMapping("/addManaVacate")
    @ResponseBody
    public boolean addManaVacate(@RequestBody UserVocate userVacate, HttpSession session) {
        System.out.println(userVacate.toString());
        LoginUser loginUser = (LoginUser) session.getAttribute("loginUser");
        userVacate.setEmpNo(loginUser.getAccount());
        userVacate.setUserName(loginUser.getUserName());
        userVacate.setStatus(0);
        int i = userService.addVocate(userVacate);
        if (i > 0) {
            return true;
        } else {
            return false;
        }
    }


    @RequestMapping("/toVacateMana")
    public String toVacateMana() {
        return "vacateMana.html";
    }

    @RequestMapping("/getAllVacate")
    @ResponseBody
    public Map<String, Object> getAllVacate() {
        List<UserVocate> list = manaService.selectAllVacate();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", "0");
        map.put("data", list);
        map.put("msg", "");
        map.put("count", list.size());
        return map;
    }

    @RequestMapping("/searchVacateByKeyWord")
    @ResponseBody
    public Map<String, Object> searchVacateByKeyWord(@RequestParam String empNo) {
        List<UserVocate> list = manaService.selectVacateByKeyWord(empNo);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", "0");
        map.put("data", list);
        map.put("msg", "");
        map.put("count", list.size());
        return map;
    }

    @RequestMapping("/editVacate")
    @ResponseBody
    public boolean editVacate(@RequestBody UserVocate userVocate) {
        int i = manaService.updateVacate(userVocate);
        if (i > 0) {
            return true;
        } else {
            return false;
        }
    }

    @RequestMapping("/delVacate")
    @ResponseBody
    public boolean delVacate(@RequestParam int id) {
        int i = manaService.deleteVacate(id);
        if (i > 0) {
            return true;
        } else {
            return false;
        }
    }


    //薪资管理
    @RequestMapping("/toWageMana")
    public String toWageMana() {
        return "wageMana.html";
    }

    @RequestMapping("/toWageCompute")
    public String toWageCompute() {
        return "wageCompute.html";
    }

    @RequestMapping("/getAllWages")//获取所有工资信息
    @ResponseBody
    public Map<String, Object> getAllWages(@RequestParam int page, @RequestParam int limit) {

        //	List<Wage> reList=new ArrayList<Wage>();
        int offset = (page - 1) * limit;
//		for(int i=0,j=(page-1)*limit;i<10;i++,j++) //下标越界
//			reList.add(list.get(j));
//		}
        //list.get((page-1)*limit)
        int count = manaService.selectWageCount();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("limit", limit);
        map.put("offset", offset);
        List<Wage> list = manaService.selectAllWages(map);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", list);
        map.put("code", "0");
        return map;
    }

    @RequestMapping("/searchWageByKeyWord")
    @ResponseBody
    public Map<String, Object> searchWageKeyWord(@RequestParam String empNo, @RequestParam String date) {
        Map<String, Object> map = new HashMap<String, Object>();
        System.out.println("keyword=" + empNo + "," + date);
        map.put("empNo", empNo);
        map.put("date", date);
        List<Wage> list = manaService.selectWageByKeyWord(map);
        System.out.println(list.toString());
        map.put("data", list);
        map.put("count", list.size());
        map.put("msg", "");
        map.put("code", "0");
        return map;
    }

    @RequestMapping("/delWages")
    @ResponseBody
    public boolean delWages(@RequestParam int id) {
        int i = manaService.deleteWages(id);
        if (i > 0) {
            return true;
        } else {
            return false;
        }
    }

    @RequestMapping("/addWages")
    @ResponseBody
    public boolean addWages(@RequestBody Wage wage) {
        int i = manaService.addWages(wage);
        if (i > 0) {
            return true;
        } else {
            return false;
        }
    }

    @RequestMapping("/editWages")
    @ResponseBody
    public boolean editWages(@RequestBody Wage wage) {
        System.out.println("修改后的工资信息：" + wage.toString());
        int i = manaService.updateWages(wage);
        if (i > 0) {
            return true;
        } else {
            return false;
        }
    }


    @RequestMapping("/getWages")//自动生成上月工资
    @ResponseBody
    public Map<String, Object> getWages(@RequestParam int limit, @RequestParam int page) {
        //在这里放入查询工资的条件
        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);//不用加1，因为计算的是上月薪资
        String yearAndMonth = "";
        if (month < 10) {
            yearAndMonth = year + "-" + "0" + month;
        } else {
            yearAndMonth = year + "-" + month;
        }
        int offset = (page - 1) * limit;
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("yearAndMonth", yearAndMonth);
        map.put("limit", limit);
        map.put("offset", offset);
        List<Wage> list = manaService.selectThisMonthWages(map);//自动生成上月工资
        System.out.println(list);
        int count = manaService.selectEmpCount();
        List<Wage> list1 = new ArrayList<Wage>();
        for (Wage wage : list) {
            if (wage.getSlary() - 5000 < 0) {//起征点为5000元
                wage.setTax(0);
                wage.setRealWages(wage.getRealWages() + wage.getTax());
            }
            //list1.add(wage);
//			}else {
//				//wage.setRealWages(wage.getRealWages()-wage.getTax());
//				//list1.add(wage);
//			}
            wage.setYearAndMonth(yearAndMonth);
            list1.add(wage);
        }
        System.out.println(list1.toString());
        map.put("code", "0");
        map.put("msg", "");
        map.put("count", list1.size());
        map.put("data", list1);
        return map;
    }

    @RequestMapping("/getWagesByMonth")
    @ResponseBody
    public Map<String, Object> getWagesByMonth(@RequestParam String month) {
        //前端用Layui的month来获取月份
        Map<String, Object> map = new HashMap<String, Object>();
        List list = manaService.selectWagesByMonth(month);
        map.put("code", "0");
        map.put("msg", "");
        map.put("count", list.size());
        map.put("data", list);
        return map;
    }


    //批量保存薪资信息
    @RequestMapping("/addCheckedWages")
    @ResponseBody
    @Transactional
    public boolean addCheckedWages(@RequestBody List<Wage> data) {
        int i = 0;
        for (Wage wage : data) {
            manaService.addCheckedWages(wage);    //还得判断哪些保存成功了哪些保存失败了
            i++;
        }
        if (i == data.size()) {
            return true;
        } else {
            return false;
        }
    }

    //批量删除薪资信息
    @RequestMapping("/delCheckedWages")
    @ResponseBody
    public boolean delCheckedWages(@RequestBody List<Wage> data) {
        int i = 0;
        for (Wage wage : data) {
            manaService.deleteCheckedWages(wage.getId());
            i++;
        }
        if (i == data.size()) {
            return true;
        } else {
            return false;
        }
    }

    //获取在招职位信息
    @RequestMapping("/getOpenPosition")
    @ResponseBody
    public Map<String, Object> getOpenPosition() {
        List<Position> list = manaService.selectOpenPosition();
        System.out.println(list.toString());
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", "0");
        map.put("data", list);
        map.put("msg", "");
        map.put("count", list.size());
        return map;
    }

    //备忘录操作
    @RequestMapping("/getMemo")
    @ResponseBody
    public Map<String, Object> getMemo(HttpServletRequest request) {
        HttpSession session = request.getSession();
        LoginUser loginUser = (LoginUser) session.getAttribute("loginUser");
        String empNo = loginUser.getAccount();
        List<Memo> list = manaService.selectMemoByEmpNo(empNo);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", "0");
        map.put("data", list);
        map.put("msg", "");
        map.put("count", list.size());
        return map;
    }

    @RequestMapping("/deleteMemo")
    @ResponseBody
    public boolean deleteMemo(@RequestParam int id) {
        int i = manaService.deleteMemo(id);
        if (i > 0) {
            return true;
        } else {
            return false;
        }
    }

    //通讯录
    @RequestMapping("/toAddressList")
    public String toAddressList() {
        return "addressList.html";
    }

    @RequestMapping("/getAddressList")
    @ResponseBody
    public Map<String, Object> getAddressList() {
        Map<String, Object> map = new HashMap<String, Object>();
        List<AddressList> list = manaService.selectAddressList();
        map.put("code", "0");
        map.put("data", list);
        map.put("msg", "");
        map.put("count", list.size());
        return map;
    }

    @RequestMapping("/searchAddressList")
    @ResponseBody
    public Map<String, Object> searchAddressList(@RequestParam String likeEmpNo) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<AddressList> list = manaService.selectAddressListByKeyWord(likeEmpNo);
        map.put("code", "0");
        map.put("data", list);
        map.put("msg", "");
        map.put("count", list.size());
        return map;
    }
}
